package com.tys.algorithm.advanced.code.class02;

public class Code01_Swap {
    public static void main(String[] args) {
        int a = 16;
        int b = 603;
        System.out.println(a + " " + b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println(a + " " + b);


        System.out.println("=====================");
        //注意数组中如果相同下标则会出错s
        //arr[] 指向相同区域，arr[0]^arr[0]之后arr[0]=0，后续计算则是0^0 始终是0
        //a=b=16 a=a^b=0 ,b=a^b=16，可以还原回来
        int[] arr = {3, 15, 100};
        int i = 0;
        int j = 0;
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
        System.out.println(arr[i] + ", " + arr[j]);


        System.out.println(arr[0]);
        System.out.println(arr[2]);

        swap(arr, 0, 0);

        System.out.println(arr[0]);
        System.out.println(arr[2]);

        System.out.println(arr[1]);
        swap(arr, 1, 1);
        System.out.println(arr[1]);

    }


    public static void swap(int[] arr, int i, int j) {
        // arr[0] = arr[0] ^ arr[0];
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }


}
